import random
# 定义房间类
class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.players = []

    def enter_room(self, player):
        if len(self.players) < 2:
            self.players.append(player)
            player.room_id = self.room_id
            print("玩家{}进入了房间{}".format(player.player_id, self.room_id))
        else:
            print("房间{}已满，无法加入".format(self.room_id))

    def exit_room(self, player):
        if player in self.players:
            self.players.remove(player)
            player.room_id = None
            print("玩家{}退出了房间{}".format(player.player_id, self.room_id))


# 定义用户类
class Player:
    def __init__(self, player_id, gold):
        self.player_id = player_id
        self.gold = gold
        self.room_id = None

    def play(self):
        if self.room_id is None:
            print("您还没有进入任何房间")
            return
        room = rooms[self.room_id]
        if len(room.players) < 2:
            print("房间{}人数不足，无法开始游戏".format(self.room_id))
            return
        # 两个玩家都准备好了，游戏正式开始
        print("游戏正式开始，玩家{}和玩家{}对决".format(room.players[0].player_id, room.players[1].player_id))
        while True:
            # 按下任意键进行猜拳
            input("按下任意键进行猜拳")
            p1 = random.choice(["石头", "剪刀", "布"])
            p2 = random.choice(["石头", "剪刀", "布"])
            print("玩家{}出了{}".format(room.players[0].player_id, p1))
            print("玩家{}出了{}".format(room.players[1].player_id, p2))
            # 判断胜负
            if p1 == p2:
                print("平局")
            elif (p1 == "石头" and p2 == "剪刀") or (p1 == "剪刀" and p2 == "布") or (p1 == "布" and p2 == "石头"):
                # 玩家1获胜
                print("玩家{}获胜".format(room.players[0].player_id))
                room.players[0].gold += 100
                room.players[1].gold -= 100
            else:
                # 玩家2获胜
                print("玩家{}获胜".format(room.players[1].player_id))
                room.players[0].gold -= 100
                room.players[1].gold += 100
            # 判断金豆是否充足
            if room.players[0].gold <= 0:
                print("玩家{}金豆已经输光，游戏结束".format(room.players[0].player_id))
                self.exit_room(room.players[0])
                self.exit_room(room.players[1])
                break
            elif room.players[1].gold <= 0:
                print("玩家{}金豆已经输光，游戏结束".format(room.players[1].player_id))
                self.exit_room(room.players[0])
                self.exit_room(room.players[1])
                break


# 创建两个房间
rooms = {}
for i in range(2):
    room = Room(i)
    rooms[i] = room
# 创建三个玩家
players = []
for i in range(3):
    player = Player(i, 500)
    players.append(player)
# 让玩家1进入房间0
rooms[0].enter_room(players[0])
# 让玩家2进入房间0
rooms[0].enter_room(players[1])
# 让玩家3进入房间1
rooms[1].enter_room(players[2])
# 玩家1开始游戏
players[0].play()